﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Brain
{
    public class DefaultStereoscopicManager : IStereoscopicManager
    {
        public void DeserializedAll()
        {
        }
        private StereoMode stereoMode;
        public StereoMode StereoMode
        {
            get { return stereoMode; }
            set { stereoMode = value; Load(); }
        }

        public bool Invert { get; set; }

        public float EyeDistance = 1;

        private RenderTarget2D leftTarget;
        private RenderTarget2D rightTarget;
        private Vector2 LeftPosition;
        private Vector2 RightPosition;

        private SpriteBatch mBatch;

        private void Load()
        {
            TextureUtil.Dispose(this.leftTarget);
            TextureUtil.Dispose(this.rightTarget);
            
            if (stereoMode != Brain.StereoMode.NoStereo)
            {
                Vector2 leftSize, rightSize;

                GraphicsDevice device = Engine.Instance.GraphicsDevice;
                PresentationParameters pp = device.PresentationParameters;
                StereoscopicUtil.GetFrames(pp.BackBufferWidth,
                    pp.BackBufferHeight,
                    this.stereoMode,
                    out leftSize, out LeftPosition,
                    out rightSize, out RightPosition);

                leftTarget = new RenderTarget2D(device, (int)leftSize.X, (int)leftSize.Y,
                    false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);
                rightTarget = new RenderTarget2D(device, (int)rightSize.X, (int)rightSize.Y,
                    false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);

                mBatch = new SpriteBatch(device);
            }
        }

        public void Update()
        {
            
        }

        public void Draw(ICamera3D Camera, IRenderManager3D renderManager)
        {
            /*GraphicsDevice device = Engine.Instance.GraphicsDevice;

            device.SetRenderTarget(leftTarget);
            //renderManager.DrawEverything(Camera.View, Camera.Projection);
            device.SetRenderTarget(null);

            Vector3 right = Camera.View.Right * EyeDistance;
            Vector3 newPos = Camera.Position + right;
            Vector3 newTarget = Camera.Target + right;
            Matrix newView = Matrix.CreateLookAt(newPos, newTarget, Camera.Up);

            device.SetRenderTarget(rightTarget);
            //renderManager.DrawEverything(newView, Camera.Projection);
            device.SetRenderTarget(null);

            DrawFrames();*/
        }

        public void Draw(IRenderManager2D renderManager)
        {
            GraphicsDevice device = Engine.Instance.GraphicsDevice;

            Matrix m = StereoscopicUtil.GetFrameMatrix(this.StereoMode);

            device.SetRenderTarget(leftTarget);
            renderManager.DrawEverything(m, false);
            device.SetRenderTarget(null);

            device.SetRenderTarget(rightTarget);
            renderManager.DrawEverything(m, true);
            device.SetRenderTarget(null);

            DrawFrames();
        }

        private void DrawFrames()
        {
            mBatch.Begin();
            if (Invert)
            {
                mBatch.Draw(leftTarget, this.RightPosition, Color.White);
                mBatch.Draw(rightTarget, this.LeftPosition, Color.White);
            }
            else
            {
                mBatch.Draw(leftTarget, this.LeftPosition, Color.White);
                mBatch.Draw(rightTarget, this.RightPosition, Color.White);
            }
            mBatch.End();
        }

        public void Reset()
        {
        }

        public SerializationData Data { get; set; }

        public void SerializeData(DefaultSerializationManager manager)
        {
        }

        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
        }
    }
}
